﻿using System;
using System.Globalization;
using Business;
using UI_MasterPage;

namespace UI_Main
{
    public partial class PresentationViewArticle : System.Web.UI.Page
    {
        private ArticleBusiness _articleBusiness;
        private CommentBusiness _commentBusiness;
        protected void Page_Init(object sender, EventArgs e)
        {
            var masterPage = (PresentationMain)Master;
            if (masterPage != null)
            {
                _articleBusiness = masterPage.ArticleBusiness;
                _commentBusiness = masterPage.CommentBusiness;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["Id"] == null)
                {
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    int page = 1;
                    if (Request.QueryString["page"] != null)
                    {
                        page = Int32.Parse(Request.QueryString["page"].ToString(CultureInfo.InvariantCulture));
                    }

                    var article = _articleBusiness.GetById(Guid.Parse(Request.QueryString["Id"].ToString(CultureInfo.InvariantCulture)));
                    ArticleHidden.Value = article.Id.ToString(); ;
                    lblTile.Text = article.Title;
                    lblContent.Text = article.Content;
                    lblAuthor.Text = article.User;

                    var paginatedInfo = _commentBusiness.GetAllCommentByArticle(article.Id, page, 2);
                    PageNavigator.CurrentPage = paginatedInfo.CurrentPage;
                    PageNavigator.VisiblePages = 5;
                    PageNavigator.TotalPages = paginatedInfo.TotalPages;
                    PageNavigator.NavigatePage = "~/UI_Main/ViewArticle.aspx?Id=" + article.Id;

                    CommentRepeater.DataSource = paginatedInfo.Data;
                    CommentRepeater.DataBind();
                }
            }
        }

        protected void SendButton_Click(object sender, EventArgs e)
        {
            Guid id = Guid.Parse(ArticleHidden.Value);
            string message = CommentTextBox.Text;
            _commentBusiness.Add(id, "chung1991", message);
           Response.Redirect(Request.Url.AbsoluteUri);     
        }
    }
}